package com.jk.service;

import com.jk.dao.IntegralDao;
import com.jk.entity.IntegralEntity;
import com.jk.entity.ItypeEntity;
import com.jk.entity.PeopleEntity;
import com.jk.entity.UserEntity;
import com.jk.utils.Daoru;
import com.jk.utils.PoiUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class IntegralService {

    @Autowired
    private IntegralDao integralDao;
    /*
     * @Author 曾义才
     * @Description //TODO 查询
     * @Date 17:47 2021/4/21
     * @Param [integralEntity]
     * @return java.util.List<com.jk.entity.IntegralEntity>
     */
    public List<IntegralEntity> query(IntegralEntity integralEntity) {
        return  integralDao.query(integralEntity);
    }
    /*
     * @Author 曾义才
     * @Description //TODO 动态下拉
     * @Date 17:48 2021/4/21
     * @Param []
     * @return java.util.List<com.jk.entity.ItypeEntity>
     */
    public List<ItypeEntity> getType() {
        return  integralDao.getType();
    }
    /*
     * @Author 曾义才
     * @Description //TODO 回显
     * @Date 17:48 2021/4/21
     * @Param [id]
     * @return com.jk.entity.IntegralEntity
     */
    public IntegralEntity gethui(Integer id) {
        return  integralDao.getHui(id);
    }

    public void update(IntegralEntity integralEntity) {
        integralDao.update(integralEntity);
    }
    /*
     * @Author 曾义才
     * @Description //TODO 暂停
     * @Date 17:48 2021/4/21
     * @Param [id]
     * @return void
     */
    public void zan(Integer id) {
        integralDao.zan(id);

    }
   /*
    * @Author 曾义才
    * @Description //TODO 删除
    * @Date 17:48 2021/4/21
    * @Param [id]
    * @return void
    */
    public void del(Integer id) {
        integralDao.del(id);
    }
   /*
    * @Author 曾义才
    * @Description //TODO 导出
    * @Date 17:48 2021/4/21
    * @Param [response]
    * @return void
    */
    public void poidaochu(HttpServletResponse response) {
         List<IntegralEntity> list=integralDao.cha();
         List<ItypeEntity> ltype=integralDao.type();
        XSSFWorkbook workbook = new XSSFWorkbook();
        //定义sheet页名称
        String sheetName = "日志记录";
//		定义标题数组
        String[] titles = {"序号","类别","标题","分数","描述","状态"};
//		调工具类，得到已经写好标题的sheet页
        XSSFSheet sheet = PoiUtil.exportData(workbook, sheetName, titles );
        for (int i = 0; i < list.size(); i++) {
            IntegralEntity info=list.get(i);
            XSSFRow row1 = sheet.createRow(i + 1);// 1、2、3...
            row1.createCell(0).setCellValue(info.getId());
            for (int j = 0; j < ltype.size(); j++) {
                if(info.getLei()==ltype.get(j).getTid()){
                    row1.createCell(1).setCellValue(ltype.get(j).getT_lei());
                }
            }
            row1.createCell(2).setCellValue(info.getBiao());
            row1.createCell(3).setCellValue(info.getFen());
            row1.createCell(4).setCellValue(info.getFen());
            if(info.getZhuang()==1){
                row1.createCell(5).setCellValue("启用");
            }else{
                row1.createCell(5).setCellValue("暂停");
            }
        }
        PoiUtil.down(response, workbook, "积分标准制定记录表.xlsx");
    }
    /*
     * @Author 曾义才
     * @Description //TODO 批量修改类型
     * @Date 17:49 2021/4/21
     * @Param [ids]
     * @return void
     */
    public void updall(Integer[] ids,Integer lei) {
        System.out.println(lei);
        for (int i = 0; i < ids.length; i++) {
            integralDao.updall(ids[i],lei);
        }

    }

    public void add(IntegralEntity integralEntity) {
        integralDao.add(integralEntity);
    }

    public List<PeopleEntity> querypeople(HttpServletRequest request) {
        UserEntity user= (UserEntity) request.getSession().getAttribute("user");
        return  integralDao.querypeople();
    }

    public List<ItypeEntity> queryGuan(HttpServletRequest request) {
        return  integralDao.queryGuan();
    }

    public void addGuan(ItypeEntity itypeEntity) {
        integralDao.addGuan(itypeEntity);

    }

    public void updateGuan(ItypeEntity itypeEntity) {
        integralDao.updateGuan(itypeEntity);
    }

    public ItypeEntity getGuan(Integer tid) {
        return  integralDao.getGuan(tid);
    }

    public void delGuan(Integer tid) {
        integralDao.delGuan(tid);
    }

    public void hui(Integer id) {
        integralDao.hui(id);
    }

    public List<IntegralEntity> queryZan(IntegralEntity integralEntity) {
        return  integralDao.queryZan(integralEntity);
    }



/*   public void daoru(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        List<ItypeEntity> ltype=integralDao.type();
        try {
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheetAt = workbook.getSheetAt(0);
            int last=sheetAt.getLastRowNum();
            List<IntegralEntity> list=new ArrayList<IntegralEntity>();
            for (int i = 0; i < last; i++) {
                IntegralEntity one=new IntegralEntity();
                XSSFRow row = sheetAt.getRow(i+1);
                for (int j = 0; j < ltype.size(); j++) {
                    if(row.getCell(1).getStringCellValue().equals(ltype.get(i).getT_lei())){
                        one.setLei(ltype.get(i).getTid());
                    }
                }
                one.setBiao(row.getCell(2).getStringCellValue());
                one.setFen(row.getCell(3).getStringCellValue());
           *//*     book.setBook_name(row.getCell(0).getStringCellValue());
                book.setBook_img(row.getCell(1).getStringCellValue());
                book.setBook_author(row.getCell(2).getStringCellValue());
                book.setBook_count((int) row.getCell(3).getNumericCellValue());
                book.setBook_type((int) row.getCell(4).getNumericCellValue());*//*
                list.add(book);
            }
            for (int i = 0; i < list.size(); i++) {
                bookDao.daoru(list.get(i));
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }*/
}
